// pages/updateBank/updateBank.js
var app = getApp();
const util = require('../../utils/util')
const images = require("../../images/bank/bank-sel/base64_images")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    banks:[],//银行名
    list:[],//图片名
    bankIdx:null,
    cardholder:'',//持卡人
    bankID:""
  },
  bindPickerChange:function(e){
    this.setData({bankIdx:e.detail.value})
  },
  bindNameInput:function(e){
    this.setData({
      cardholder:e.detail.value
    })
  },
  bindBankInput:function(e){
    this.setData({
      bankID:e.detail.value
    })
  },
  bindSubmit:function(){
    let that = this
    if (this.data.bankIdx!=0&&this.data.bankID&&this.data.cardholder) {
      util.post({
        url: 'https://groomtea.icebartech.com/api/miniApp/salesman/updateBank', 
        data: {
          "bankCard": that.data.bankID,
          "cardholder": that.data.cardholder,
          "openBank":  that.data.list[that.data.bankIdx].slice(4)
        },
        success:(res)=> {
          console.log(res)
          wx.navigateBack({ delta: 1, success: () => { app.titleToast("修改成功")}})
        }
      })
    }else{
      let toastTitle=""
      if (this.data.bankIdx==0) {
        toastTitle = "请选择开户银行"
      }else if(this.data.bankID==""){
        toastTitle ="请输入银行卡号"
      } else if (this.data.cardholder==""){
        toastTitle = "请输入持卡人姓名"
      }
      app.titleToast(toastTitle);
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let bankID = options.bankCard||"";
    let cardholder = options.cardholder||"";
    let openBank = 'bank' + options.openBank|| null;
    var selectedIndex = null;
    let names = []
    let list = []
    console.log("selected:"+bankID,cardholder,openBank)
    Object.keys(images.banks).forEach((value,index) => {
      console.log(index,images.banks[value].name)
      names[index] = images.banks[value].name
      list[index] = value
      if(openBank&&openBank==value)
        selectedIndex = index
    });
    console.log(list,names)
    this.setData({
      banks:names,
      list:list,
      bankID,
      cardholder,
      bankIdx:selectedIndex
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})